﻿using BZ.Logger;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_转盘下料 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘下料 Instance = new Station_转盘下料();
        public Stopwatch SunWay10 = new Stopwatch();
        public string StationName = "Station10转盘下料";

        public bool isHaveProduct;
        public bool IsStartOnce;
        public bool IsStartOnce1;
        public bool IsClearModelDone;
        //步骤字段、属性
        private StationStep _Step = StationStep.轴移动至取料高位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            IsStartOnce = false;
            logBaseInfo = "转盘下料流程";
            Step = StationStep.轴移动至取料高位;
            SunWay10.Start();
        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.轴移动至取料高位:
                        if (MotionControl.AxesMove(Machine.pos_取料高位_转盘下料工位))
                        {
                            Step = StationStep.等待前左右工位允许取料;
                        }
                        break;

                    case StationStep.等待前左右工位允许取料:
                        if ((Station_转盘八穴.Instance.SignalComplete || (Common.IsClearModel && Common.TurnTableNone && !IsClearModelDone&& !Station_下料搬运.Instance.SignalRequest1) || (IsStartOnce1 && IsStartOnce)) && Station_下置激光.Instance.SignalComplete)
                        {
                            if (IsStartOnce && IsStartOnce1)
                            {
                                IsStartOnce = false;
                                IsStartOnce1 = false;
                                isHaveProduct = false;
                            }
                            else
                            {
                                IsStartOnce1 = true;
                                if (Common.IsClearModel && Common.TurnTableNone)
                                {
                                    isHaveProduct = false;
                                    IsClearModelDone = true;
                                }
                                else
                                {
                                    isHaveProduct = true;
                                }
                            }
                        Step = StationStep.轴移动至取料位;
                        }
                        break;

                    case StationStep.轴移动至取料位:
                        if (MotionControl.AxesMove(Machine.pos_取料位_转盘下料工位))
                        {
                            Step = StationStep.打开真空吸;
                        }
                        break;

                    case StationStep.打开真空吸:
                        if (VacuumMethod(true, (Machine.do_转盘下料真空吸1, Machine.di_转盘下料真空检测1, Station_转盘八穴.Instance.SignalComplete && (!Common.IsEmptyModel && isHaveProduct)),
                                               (Machine.do_转盘下料真空吸2, Machine.di_转盘下料真空检测2, (Station_下置激光.Instance.IsHaveProduct && !Common.IsEmptyModel && !Common.GRR循环模式))))
                        {
                            SignalRequest = true;
                            Step = StationStep.等待转盘关真空吸;
                        }
                        break;

                    case StationStep.等待转盘关真空吸:
                        if (!Station_转盘八穴.Instance.SignalComplete && !Station_下置激光.Instance.SignalComplete)
                        {
                            Step = StationStep.轴移动至取料高位1;
                        }
                        break;

                    case StationStep.轴移动至取料高位1:
                        if (MotionControl.AxesMove(Machine.pos_取料高位_转盘下料工位))
                        {
                            SignalRequest = false;
                            SignalComplete = true;
                            Step = StationStep.等待后左右工位允许放料;
                        }
                        break;

                    case StationStep.等待后左右工位允许放料:
                        if (Station_下置激光.Instance.SignalRequest && (Station_产品扫码.Instance.SignalRequest || Station_下料搬运.Instance.SignalRequest1))
                        {
                            Station_下料搬运.Instance.SignalRequest1 = false;
                            Step = StationStep.轴移动至放料高位1;
                        }
                        break;

                    case StationStep.轴移动至放料高位1:
                        if (MotionControl.AxesMove(Machine.pos_放料高位_转盘下料工位))
                        {
                            Step = StationStep.轴移动到放料位;
                        }
                        break;

                    case StationStep.轴移动到放料位:
                        if (MotionControl.AxesMove(Machine.pos_放料位_转盘下料工位))
                        {
                            Step = StationStep.关闭真空吸;
                        }
                        break;

                    case StationStep.关闭真空吸:
                        Machine.do_转盘下料真空吸1.OFF();
                        Machine.do_转盘下料真空吸2.OFF();
                        Machine.do_转盘下料真空破1.ON();
                        Machine.do_转盘下料真空破2.ON();
                        Thread.Sleep(50);
                        Machine.do_转盘下料真空破1.OFF();
                        Machine.do_转盘下料真空破2.OFF();
                        if (!Common.GRR循环模式)
                        {
                            Station_产品扫码.Instance.IsHaveProduct = Station_下置激光.Instance.IsHaveProduct;
                        }
                        Station_下置激光.Instance.IsHaveProduct = isHaveProduct;
                        Step = StationStep.放料并通知完成;
                        break;

                    case StationStep.放料并通知完成:
                        if (MotionControl.AxesMove(Machine.pos_放料高位_转盘下料工位))
                        {
                            SignalComplete = false;
                            Step = StationStep.等待后左右工位关闭信号;
                        }
                        break;

                    case StationStep.等待后左右工位关闭信号:
                        if (!Station_下置激光.Instance.SignalRequest && !Station_产品扫码.Instance.SignalRequest)
                        {
                            Step = Common.GRR循环模式 ? StationStep.GRR等待下工位完成 : StationStep.轴移动至取料高位;
                        }
                        break;

                    case StationStep.GRR等待下工位完成:
                        if (Station_下置激光.Instance.GRRSignalComplete)
                        {
                            Station_下置激光.Instance.GRRSignalComplete = false;
                            Step = StationStep.GRR轴移动到放料位;
                        }
                        break;
                    case StationStep.GRR轴移动到放料位:
                        if (MotionControl.AxesMove(Machine.pos_放料位_转盘下料工位))
                        {
                            Step = StationStep.GRR打开真空吸;
                        }
                        break;
                    case StationStep.GRR打开真空吸:
                        if (VacuumMethod(true, (Machine.do_转盘下料真空吸1, Machine.di_转盘下料真空检测1, Station_下置激光.Instance.IsHaveProduct && !Common.IsEmptyModel)))
                        {
                            Step = StationStep.GRR轴移动到取料高位;
                        }
                        break;
                    case StationStep.GRR轴移动到取料高位:
                        if (MotionControl.AxesMove(Machine.pos_取料高位_转盘下料工位))
                        {
                            Step = StationStep.GRR轴移动到取料位;
                        }
                        break;
                    case StationStep.GRR轴移动到取料位:
                        if (MotionControl.AxesMove(Machine.pos_取料位_转盘下料工位))
                        {
                            GRRSignalComplete = true;
                            Step = StationStep.GRR关闭真空吸;
                        }
                        break;
                    case StationStep.GRR关闭真空吸:
                        if (Station_转盘八穴.Instance.GRRSignalRequest)
                        {
                            Machine.do_转盘下料真空吸1.OFF();
                            Machine.do_转盘下料真空破1.ON();
                            Thread.Sleep(200);
                            Machine.do_转盘下料真空破1.OFF();
                            Step = StationStep.GRR轴移动到取料高位1;
                        }
                        break;

                    case StationStep.GRR轴移动到取料高位1:
                        if (MotionControl.AxesMove(Machine.pos_取料高位_转盘下料工位))
                        {
                            GRRSignalComplete = false;
                            Step = StationStep.轴移动至取料高位;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            轴移动至取料高位,
            等待前左右工位允许取料,
            轴移动至取料位,
            打开真空吸,
            等待转盘关真空吸,
            轴移动至取料高位1,
            等待后左右工位允许放料,
            轴移动至放料高位1,
            轴移动到放料位,
            关闭真空吸,
            放料并通知完成,
            等待后左右工位关闭信号,
            GRR等待下工位完成,
            GRR轴移动到放料位,
            GRR打开真空吸,
            GRR轴移动到取料高位,
            GRR轴移动到取料位,
            GRR关闭真空吸,
            GRR轴移动到取料高位1,
        }
    }

}
